package com.atguigu.rabbitmq.three;

import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;

import java.util.Scanner;

/**
 * ClassName: Task2
 * Description: 消息在手动应答时是不丢失，放回队列中重新消费
 * Date: 2023/04/27 PM 4:17:00
 * <p>
 * project: atguigu-rabbitmq
 * package: com.atguigu.rabbitmq.three
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
public class Task2 {

    /**
     * 队列名称
     */
    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.confirmSelect();
        // 声明队列
        // 需要让 Queue 队列持久化
        boolean durable = true;
        channel.queueDeclare(TASK_QUEUE_NAME, durable, false, false, null);
        // 从控制台中输入消息
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String message = scanner.next();
            // 设置生产者发送消息为持久化消息(要求保存到磁盘上)
            channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
            System.out.println("生产者发出消息：" + message);
        }
    }
}
